package com.lupan.tomcatStudy.activemq.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.jms.*;

/**
 * 说明：详细接受方
 *
 * @author lupan
 * @version 2016/5/30 17:58
 */
@Component
public class JmsConsumer {

    @Autowired
    @Qualifier("jmsTopicTemplate")
    private JmsTemplate jmsTemplate;

    /**
     * 监听到消息目的有消息后自动调用onMessage(Message message)方法
     */
    public void recive() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
        Destination destination = (Destination) applicationContext.getBean("destination");
        while (true) {
            try {
                TextMessage txtmsg = (TextMessage) jmsTemplate
                        .receive(destination);
                if (null != txtmsg) {
                    System.out.println("[DB Proxy] " + txtmsg);
                    System.out.println("[DB Proxy] 收到消息内容为: "
                            + txtmsg.getText());
                } else
                    break;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
